/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.quickstart.slice;

import com.wordplat.ikvstockchart.InteractiveKLineView;
import com.wordplat.ikvstockchart.compat.PerformenceAnalyser;
import com.wordplat.ikvstockchart.compat.ViewUtils;
import com.wordplat.ikvstockchart.drawing.*;
import com.wordplat.ikvstockchart.entry.*;
import com.wordplat.ikvstockchart.marker.XAxisTextMarkerView;
import com.wordplat.ikvstockchart.marker.YAxisTextMarkerView;
import com.wordplat.ikvstockchart.render.KLineRender;
import com.wordplat.quickstart.ResourceTable;
import com.wordplat.quickstart.base.BaseAbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class MACD_RSI_KDJ_Show_Together_Slice extends BaseAbilitySlice {
    private InteractiveKLineView kLineView;

    private KLineRender kLineRender;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_macd_rsi_kdj_show_together);
        initViews();
        loadKLineData();
    }

    private void initViews() {
        kLineView = (InteractiveKLineView) findComponentById(ResourceTable.Id_kLineView_macd_rsi_kdj_show);

        kLineView.setEnableLeftRefresh(false);
        kLineRender = (KLineRender) kLineView.getRender();

        final int paddingTop = ViewUtils.vpToPx(this, 10);
        final int stockMarkerViewHeight = ViewUtils.vpToPx(this, 15);

        /**
         * MACD
         */
        HighlightDrawing macdHighlightDrawing = new HighlightDrawing();
        macdHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        StockMACDIndex macdIndex = new StockMACDIndex();
        macdIndex.addDrawing(new MACDDrawing());
        macdIndex.addDrawing(new StockIndexYLabelDrawing());
        macdIndex.addDrawing(macdHighlightDrawing);
        macdIndex.setPaddingTop(paddingTop);
        kLineRender.addStockIndex(macdIndex);

        /**
         * RSI
         */
        HighlightDrawing rsiHighlightDrawing = new HighlightDrawing();
        rsiHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        StockRSIIndex rsiIndex = new StockRSIIndex();
        rsiIndex.addDrawing(new RSIDrawing());
        rsiIndex.addDrawing(new StockIndexYLabelDrawing());
        rsiIndex.addDrawing(rsiHighlightDrawing);
        rsiIndex.setPaddingTop(paddingTop);
        kLineRender.addStockIndex(rsiIndex);

        /**
         * KDJ
         */
        HighlightDrawing kdjHighlightDrawing = new HighlightDrawing();
        kdjHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        StockKDJIndex kdjIndex = new StockKDJIndex();
        kdjIndex.addDrawing(new KDJDrawing());
        kdjIndex.addDrawing(new StockIndexYLabelDrawing());
        kdjIndex.addDrawing(kdjHighlightDrawing);
        kdjIndex.setPaddingTop(paddingTop);
        kLineRender.addStockIndex(kdjIndex);

        kLineRender.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));
        kLineRender.addMarkerView(new XAxisTextMarkerView(stockMarkerViewHeight));
    }

    private void loadKLineData() {
        TaskDispatcher globalTaskDispatcher = getMainTaskDispatcher();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                EntrySet entrySet;
                PerformenceAnalyser.getInstance().addWatcher();
                String kLineData = "";
                ResourceManager resourceManager = getContext().getResourceManager();
                RawFileEntry rawFileEntry = resourceManager.getRawFileEntry("resources/rawfile/kline1.txt");
                Resource resource = null;
                try {
                    resource = rawFileEntry.openRawFile();
                    byte[] buffer = new byte[resource.available()];
                    resource.read(buffer);
                    kLineData = new String(buffer, StandardCharsets.UTF_8);
                } catch (IOException e) {
                    String ss = e.toString();
                }
                PerformenceAnalyser.getInstance().addWatcher();
                entrySet = StockDataTest.parseKLineData(kLineData);
                entrySet.computeStockIndex();
                kLineView.setEntrySet(entrySet);
                kLineView.notifyDataSetChanged();
            }
        };
        globalTaskDispatcher.delayDispatch(runnable,1000);
    }
}
